package com.example.onion.hxchat.activity;


import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.example.onion.hxchat.adapter.MyCommentAdapter;
import com.example.onion.hxchat.fragment.TabViewPagerFragment;
import com.example.onion.hxchat.mvvm.UserViewModel;
import com.example.onion.hxchat.room.entity.Info;
import com.example.onion.hxchat.room.entity.User;
import com.example.onion.hxchat.test.CommentBean;
import com.example.onion.hxchat.util.PassWordHelp;
import com.example.superyi.myteam.R;
import com.example.wjy.viewmodels.MasterViewModel;
import com.hyphenate.chat.EMClient;

import java.util.List;

/**
 * 嵌套一个TabViewPagerFragment(也是wc的最主要的fragment)
 * 包括显示三个fragment，
 * 消息InfoFragment，好友列表FriendFragment，通知列表NotificationFragment三个fragment
 */
public class InfoActivity extends AppCompatActivity implements View.OnClickListener {
    private Toolbar toolbar;
    private final String MAIN_FRAGMENT = "mainFragment";//fragment的标识
    private FragmentManager fragmentManager;
    private String TAG = "InfoActivity";
    private RecyclerView recyclerView;
    private List<User> mList;
    private Button mBtn;
    private Button mBtn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏项目名
        setContentView(R.layout.activity_info);
        //initView();
        recyclerView = findViewById(R.id.infoactivity_recyclerview);
        MyCommentAdapter adapter = new MyCommentAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(InfoActivity.this));
        recyclerView.setAdapter(adapter);
        UserViewModel userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
        userViewModel.getAllUsers().observe(this, new Observer<List<User>>() {
            @Override
            public void onChanged(List<User> users) {
                adapter.setmList(users);
                adapter.notifyDataSetChanged();
            }
        });
        mBtn = findViewById(R.id.infoActivity_insert_btn);
        mBtn1 = findViewById(R.id.infoActivity_delete_btn);
        mBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userViewModel.deleteALltUser();
            }
        });
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                userId;
//                this.userName = userName;
//                this.userAge = userAge;
//                this.userGrade = userGrade;
//                this.userSpy = userSpy;
//                this.userEmail = userEmail;
//                this.userSex = userSex;
//                this.userAbility = userAbility;
                for (int i = 0; i < 10; i++) {
                    userViewModel.insertUser(new User("id"+i, "name", 1, "grade", "spy", "email", "sex", "ability"));
                }
            }
        });
    }

    private void initView() {
        //加载布局一定要正确  重复指挥被一个活动运用 当前活动可能显示不出来
        toolbar = findViewById(R.id.info_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }

    }

    /**
     * 集成环信退出登录 EMClient.getInstance().logout(true)；
     * 顺便实现删除保存的登录的用户的账号，完成LoginActivity类说明的功能
     */
    private void logout() {
        //将空值的用户账户和密码保存，覆盖先前的数据实现清除账户数据，
        // 以便可以通过PassWordHelp能否获取数据判断用户登录状态
        PassWordHelp.savePassword(InfoActivity.this, "", "", false);
        String accountId = PassWordHelp.readPassword(InfoActivity.this)[0];
        String pwd = PassWordHelp.readPassword(InfoActivity.this)[1];
        Log.e("INfoActivity", "保存null之后获取的accountId" + accountId);
        Log.e("INfoActivity", "保存null之后获取的pwd" + pwd);
        EMClient.getInstance().logout(true);
        Intent intent = new Intent(InfoActivity.this, LoginActivity.class);
        startActivity(intent);
        Toast.makeText(InfoActivity.this, "退出登录", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //这里指定当前活动在上面要显示的导航栏菜单的资源xml
        getMenuInflater().inflate(R.menu.toolbar_main, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView mSearchView = (SearchView) menu.findItem(R.id.info_search).getActionView();
        // Assumes current activity is the searchable activity
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        //搜索框
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {//String query提交的搜索内容
                Log.i(TAG, query);
                Toast.makeText(InfoActivity.this, query, Toast.LENGTH_SHORT).show();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {//回调查询文本的更改。
                return false;
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home://系统自带的返回箭头的资源id
                finish();
                Log.d("Infoactivity", "返回 ");
                break;
//            case R.id.info_search:
//                Log.d("InfoActivity", "搜索");
//                Toast.makeText(InfoActivity.this, "搜索", Toast.LENGTH_SHORT).show();
//                break;
            case R.id.main_toolbar_logout://退出登录的选项
                Log.d("InfoActivity", "退出登录");
                logout();
                break;
            default:
                break;
        }
        return true;
    }


    @Override
    public void onClick(View v) {

    }

}
